//
//  PATitleBar.m
//  PAMenuDemo
//
//  Created by Pay_SMP002 on 13-6-9.
//  Copyright (c) 2013年 平安付. All rights reserved.
//

#import "PATitleBar.h"

@implementation PATitleBar


- (id)initWithFrame:(CGRect)frame
{
    return [self initWithFrame:frame mode:TitleBarMode_Defult];
}

- (id)initWithFrame:(CGRect)frame mode:(TitleBarMode)mode
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        nMode = mode;

        bgImageView = [[UIImageView alloc] initWithFrame:self.bounds];
        [self addSubview:bgImageView];


        CGSize size = frame.size;
        CGFloat x, y, w, h;
        x = 10;
        w = 50;
        h = 41;
        y = 3;

        leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
        leftButton.frame = CGRectMake(x, y, w, h);
        [leftButton addTarget:self
                       action:@selector(buttonActive:)
             forControlEvents:UIControlEventTouchUpInside];
        leftButton.tag = 0;
        [self addSubview:leftButton];

        x = size.width - w - 20;
        rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
        rightButton.frame = CGRectMake(x, y, w, h);
        [rightButton addTarget:self
                       action:@selector(buttonActive:)
             forControlEvents:UIControlEventTouchUpInside];
        rightButton.tag = 1;
        [self addSubview:rightButton];

        x = CGRectGetMaxX(leftButton.frame) + 20;
        y = 0;
        w = size.width - 2 * x;
        h = size.height;
        titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, w, h)];
        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.adjustsFontSizeToFitWidth = YES;
        titleLabel.textColor = [UIColor whiteColor];
        titleLabel.textAlignment = UITextAlignmentCenter;
        [titleLabel setFont:[UIFont boldSystemFontOfSize:24]];

        [self addSubview:titleLabel];
    }
    return self;
}

- (void)setMode:(TitleBarMode)mode {
    nMode = mode;
}

- (void)setBackgroundImage:(UIImage *)backgroundImage {
    bgImageView.image = backgroundImage;
}

- (void)setTitle:(NSString *)title {
    [titleLabel setText:title];
}

- (void)setLeftButtonImage:(UIImage *)leftImage right:(UIImage *)rightImage {
    if (leftImage) {
        [leftButton setBackgroundImage:leftImage forState:UIControlStateNormal];
    }
    if (rightImage) {
        [rightButton setBackgroundImage:rightImage forState:UIControlStateNormal];
    }
}

- (void)buttonActive:(id)sender {
    int atag = [sender tag];
    if (self.parentController && [self.parentController respondsToSelector:@selector(titleBarCallBack:)]) {
        [self.parentController titleBarCallBack:atag];
    }
}

@end
